package br.com.moove.bananasfit.view;

import org.json.JSONArray;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import br.com.moove.bananasfit.R;
import br.com.moove.bananasfit.model.IBuscarPessoasJuridicasListener;
import br.com.moove.bananasfit.model.OnServicoClickListener;
import br.com.moove.bananasfit.model.PessoaFisica;

public class FiltroPessoaJuridicaFragment extends Fragment implements
		OnClickListener {

	Button btnFiltrar;
	EditText txtNomePf;
	Spinner spnServicos;

	String[] servicos;
	int[] idServicos;
	
	PessoaFisica usuario;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View layout = inflater.inflate(
				R.layout.control_lista_p_juridica_fragment, null);
		
		spnServicos = (Spinner) layout.findViewById(R.id.spnServicos);
		txtNomePf = (EditText) layout.findViewById(R.id.txt_nome_pf);
		btnFiltrar = (Button) layout.findViewById(R.id.btnPesquisar);

		servicos = getArguments().getStringArray("Servicos");
		idServicos = getArguments().getIntArray("Ids");

		ArrayAdapter<String> a = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_spinner_item, servicos);
		a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spnServicos.setAdapter(a);
		
		btnFiltrar.setOnClickListener(this);

		return layout;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	public static FiltroPessoaJuridicaFragment getInstance(String[] servicos,
			int[] ids, PessoaFisica usuario) {
		Bundle args = new Bundle();
		args.putStringArray("Servicos", servicos);
		args.putIntArray("Ids", ids);
		args.putSerializable("Usuario", usuario);
		FiltroPessoaJuridicaFragment f = new FiltroPessoaJuridicaFragment();
		f.setArguments(args);
		return f;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.btnPesquisar) {
			if (getActivity() instanceof IBuscarPessoasJuridicasListener) {
				((IBuscarPessoasJuridicasListener) getActivity()).pesquisarPF(
						idServicos[spnServicos.getSelectedItemPosition()],
						txtNomePf.getText().toString());
			}
		}
	}

}
